package com.sls.web.manager.recharge.service.impl;

import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.querybean.base.BaseQuery;
import com.sls.web.manager.recharge.pojo.NativeRechargeRule;
import com.sls.web.manager.recharge.querybean.NativeRechargeRuleQuery;
import com.sls.web.manager.recharge.service.NativeRechargeRuleService;
import com.sls.web.manager.socket.SuperService;

/**
 * @author sxf
 * 
 */
@Service("nativeRechargeRuleService")
public class NativeRechargeRuleServiceImpl extends SuperService implements
		NativeRechargeRuleService {
	private static Logger log = Logger
			.getLogger(NativeRechargeRuleServiceImpl.class);

	/**
	 * @param serviceCode
	 * @param sendObj
	 * @param userInfoVO
	 * @param classType
	 *            类型信息(如:String,为String.class)
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public <T> T callService(String serviceCode, Head head, Object sendObj,
			UserInfoVO userInfoVO, Object... classType) throws Exception {
		Gson gson = CommonUtil.gson();
		if (head == null) {
			head = new Head();
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(serviceCode);
			head.setIp(userInfoVO.getCurrentIp());
			head.setUserId(userInfoVO.getUserId());
		}
		SocketDTO requestDTO = super.send(head, sendObj);
		if (!TransmissionCode.SUCCESS.getCode().equals(
				requestDTO.getHead().getResponseCode())) {
			throw new ManagerException(requestDTO.getHead()
					.getResponseMessage());
		}
		String body = requestDTO.getBody();
		T t = null;
		if (body != null) {
			if (classType != null && classType.length > 0) {
				if (classType[0] instanceof Class) {
					t = gson.fromJson(body, (Class<T>) classType[0]);
				} else if (classType[0] instanceof Type) {
					t = gson.fromJson(body, (Type) classType[0]);
				}
			}
		}
		return t;
	}

	public String insert(NativeRechargeRule info, UserInfoVO userInfoVO)
			throws Exception {
		String id = this.callService(Constants.INSERT_NATIVERECHARGERULE, null,
				info, userInfoVO,String.class);
		return id;
	}

	@Override
	public List<NativeRechargeRule> queryList(NativeRechargeRuleQuery query,
			UserInfoVO userInfoVO) throws Exception {
		List<NativeRechargeRule> infoPage = callService(
				Constants.QUERY_NATIVERECHARGERULE_LIST, null, query,
				userInfoVO, new TypeToken<List<NativeRechargeRule>>() {
				}.getType());
		return infoPage;
	}

	public Pagination<NativeRechargeRule> queryPage(
			NativeRechargeRuleQuery query, UserInfoVO userInfoVO)
			throws Exception {
		log.info("Enter NativeRechargeRuleService queryPage method...");
		Pagination<NativeRechargeRule> infoPage = callService(
				Constants.QUERY_NATIVERECHARGERULE_PAGE, null, query,
				userInfoVO, new TypeToken<Pagination<NativeRechargeRule>>() {
				}.getType());
		log.info("Leave NativeRechargeRuleService queryPage method...");
		return infoPage;
	}

	public NativeRechargeRule queryOne(NativeRechargeRuleQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter NativeRechargeRuleService queryPage method...");
		NativeRechargeRule infoPage = callService(
				Constants.QUERY_NATIVERECHARGERULE_ONE, null, query,
				userInfoVO, NativeRechargeRule.class);
		log.info("Leave NativeRechargeRuleService queryPage method...");
		return infoPage;
	}

	public boolean update(NativeRechargeRule info, UserInfoVO userInfoVO)
			throws Exception {

		this.callService(Constants.UPDATE_NATIVERECHARGERULE, null, info,
				userInfoVO);
		return true;
	}

	public boolean delete(NativeRechargeRule info, UserInfoVO userInfoVO)
			throws Exception {
		this.callService(Constants.DELETE_NATIVERECHARGERULE, null, info,
				userInfoVO);
		return true;
	}

	public List<NativeRechargeRule> queryAllList(NativeRechargeRuleQuery query,
			UserInfoVO userInfoVO) throws Exception {
		return queryAllList(query, Constants.QUERY_NATIVERECHARGERULE_PAGE,
				userInfoVO, new TypeToken<Pagination<NativeRechargeRule>>() {
				}.getType());
	}

	/**
	 * 查询全部信息(List)
	 * 
	 * @param query
	 * @param serviceCode
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	private <T> List<T> queryAllList(BaseQuery query, String serviceCode,
			UserInfoVO userInfoVO, Object... classType) throws Exception {
		query.setCurrentPage(1);
		query.setOffset(10);
		List<T> resultList = new LinkedList<T>();
		Pagination<T> page = callService(serviceCode, null, query, userInfoVO,
				classType);
		if (page.getList() != null) {
			resultList.addAll(page.getList());
		}
		int count = page.getRowCount();
		if (query.getPageSize() == null) {
			query.setPageSize(1);
		}
		for (int i = 0; i < Math.ceil(count * 1.0 / query.getPageSize()); i++) {
			query.setCurrentPage(i + 1);
			page = callService(serviceCode, null, query, userInfoVO, classType);
			if (page.getList() != null) {
				resultList.addAll(page.getList());
			}
		}
		return resultList;
	}
}
